﻿@{
    Layout = "~/Views/Shared/_detail.cshtml";

    if (false)
    {
        <script src="~/LiberUI/Scripts/jquery.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Base.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Action.js"></script>
    }
}
<title>订单详情</title>

<link href="/SiteUI/Trade/OrderDetail.css" rel="stylesheet" />

<div id="pagePath" class="row">
    <div obj-auth="true" obj-org="supplier_deptidstr" obj-action="Trade|Order|View" class="col-lg-6 col-sm-4">
        <a class="strong" href="/Sale/Orders">销售：订单管理</a>&nbsp;>&nbsp;<a href="#">订单详情</a>
    </div>
    <div obj-auth="true" obj-org="client_deptidstr" obj-action="Trade|Order|View" class="col-lg-6 col-sm-4">
        <a class="strong" href="/Purchase/Orders">采购：订单管理</a>&nbsp;>&nbsp;<a href="#">订单详情</a>
    </div>
    <a obj-auth="true" obj-org="supplier_deptidstr" obj-action="Sale|Delivery|Create" obj-status="Active" id="lnkDelivery" target="_blank" class="LinkTo" href="/Sale/DeliveryBillEdit?oid="><i class="soicon icon-localshipping"></i>发货安排</a>
</div>

<div class="row" id="orderHead">
    <div class="col-lg-4 col-sm-6">
        <h4>交易编号：<strong id="oh-id">00001</strong></h4>
    </div>
    <div class="col-lg-4 col-sm-6">
        <h4>总金额：<strong id="oh-amount"></strong></h4>
    </div>
    <div class="col-lg-4 col-sm-6">
        <h4>状态:<span id="oh-status" class="Pendding"><i>订单状态</i></span></h4>
    </div>
    <div class="col-lg-4 col-sm-6">
        <h5>客户订单：<strong id="oh-clnt-serial">00001</strong></h5>
    </div>
    <div class="col-lg-4 col-sm-6">
        <h5>订货日期：<strong id="oh-order-date">00001</strong></h5>
    </div>
</div>

<div class="FlateSectionHead row" id="tradeAttachemenTitle">
    <i class="soicon icon-attachfile"></i>订单附件
</div>
<div class="row" id="tradeAttachment">
</div>

<div class="row" id="orderProcess">
    <div class="col-lg-4 col-sm-4">
        <h5>执行进度</h5>
        <div id="orderExecute">
            -
        </div>
    </div>
    <div class="col-lg-4 col-sm-4">
        <h5>交付进度</h5>
        <div id="orderDelivery">
            <span>已发货：</span>
            <span>已入库：</span>
        </div>
    </div>
    <div class="col-lg-4 col-sm-4">
        <h5>付款进度</h5>
        <div id="orderPay">
            -
        </div>
    </div>
</div>
<br />
<!--基于TODO模式的订单操作区域-->
<div id="tradeAction" class="row">
    <div obj-auth="true" obj-role="saler" obj-org="supplier_deptidstr" obj-action="Sale|Order|Confirm">
        <div class="todoItem ConfirmOrder Active row">
            <div class="col-lg-9 col-sm-8 col-xs-8 todoTip">您还没有确认此订单，请查阅订单详情并确认订单</div>
            <div class="col-lg-3 col-sm-4 col-xs-4 button" id="btnConfirmOrder">确认订单</div>
        </div>
        <div class="todoItem ConfirmOrder Completed row">
            <div class="col-lg-9 col-sm-9 todoTip">您已经确认此订单，请遵照交易条款执行</div>
            <div class="col-lg-3 col-sm-3 todoStatus">已确认</div>
        </div>
    </div>
    <div obj-auth="true" obj-role="buyer" obj-org="client_deptidstr" obj-action="Purchase|Order|Admin">
        <div class="todoItem ConfirmOrder Active">
            <div class="col-lg-12 col-sm-9 col-xs-8 todoTip">供应商还没有确认此订单，<a class="LinkTo" id="btnRemindSaler">点这里</a>提醒他</div>
        </div>
        <div class="todoItem ConfirmOrder Completed">
            <div class="col-lg-12 col-sm-3 col-xs-4 todoTip">供应商已经确认此订单</div>
        </div>
    </div>
</div>

<!--订单详情-->
<div class="InfoSection row expand">
    <div class="InfoSectionTitle">
        交易信息
        <i class="soicon "></i>
    </div>
    <div class="InfoSectionBody col-lg-12" id="tradeInfo">
        <div class="row ti-body">
            <div class="col-lg-4 col-sm-6">
                <div class="DataField">
                    <h5>联系人信息</h5>
                    <span class="df-title">采购单位：</span>
                    {{#if(d.client_realname){ }}
                    <span class="df-value"><a href="#" class="Company certificated" obj-id="{{d.client_idstr}}">{{d.client_realname}}</a></span>
                    {{#}else{ }}
                    <span class="df-value"><a href="#" class="Company no-certificated">{{d.invoiceheading}}</a></span>
                    {{#} }}
                </div>
                <div class="DataField">
                    <span class="df-title">联&nbsp;&nbsp;系&nbsp;&nbsp;人：</span>
                    {{#if(d.buyer){ }}
                    <span class="df-value"><a href="#" class="People certificated" objid="{{d.buyer_id}}">{{d.buyer.realname}}</a></span>
                    {{#}else{ }}
                    <span class="df-value"><a href="#" class="People no-certificated">{{d.src_buyer}}</a></span>
                    {{#} }}
                </div>
                <div class="DataField">
                    <span class="df-title">联系方式：</span>
                    <span class="df-value">{{d.buyer_phone||d.src_buyerphone}}</span>
                </div>
                <div class="DataField">
                    <span class="df-title">供&nbsp;&nbsp;应&nbsp;&nbsp;商：</span>
                    {{#if(d.supplier_realname){ }}
                    <span class="df-value"><a class="Company certificated" obj-id="{{d.supplier_idstr}}" href="#">{{d.supplier_realname}}</a></span>
                    {{#}else{ }}
                    <span class="df-value"><a class="Company no-certificated" href="#">{{d.src_supplier}}</a></span>
                    {{#} }}
                </div>
                <div class="DataField">
                    <span class="df-title">联&nbsp;&nbsp;系&nbsp;&nbsp;人：</span>
                    {{#if(d.saler){ }}
                    <span class="df-value"><a href="#" class="People certificated" objid="{{d.saler_idstr}}">{{d.saler.realname}}</a></span>
                    {{#}else{ }}
                    <span class="df-value"><a href="#" class="People no-certificated" title='未匹配到平台用户'><i class='soicon'></i>{{d.src_saler}}</a></span>
                    {{#} }}
                </div>
                <div class="DataField">
                    <span class="df-title">联系方式：</span>
                    <span class="df-value">{{d.saler_phone||d.src_salerphone}}</span>
                </div>
            </div>
            <div class="col-lg-4 col-sm-6">
                <h5>交付信息</h5>
                <div class="DataField">
                    <span class="df-title">收&nbsp;&nbsp;货&nbsp;&nbsp;人：</span>
                    <span class="df-value">
                        <span>{{d.consignee_displayname}}</span>
                        <span>{{d.consignee_phone}}</span>
                    </span>
                </div>
                <div class="DataField">
                    <span class="df-title">交付方式：</span>
                    <span class="df-value">{{d.deliverymode}}</span>
                </div>
                <div class="DataField">
                    <span class="df-title">送货地址：</span>
                    <span class="df-value">{{d.src_dlvaddress}}</span>
                </div>
                <div class="DataField">
                    <span class="df-title">送货时间：</span>
                    {{#if(d._urgent){ }}
                    <span class="df-value urgent" title="交货日期已临近，请注意跟进">{{d._dlvDate}}<i class="icon-erroroutline"></i></span>
                    {{#}else{ }}
                    <span class="df-value">{{d._dlvDate}}</span>
                    {{#} }}
                </div>
                <div class="DataField">
                    <span class="df-title">施工单位：</span>
                    <span class="df-value">{{d._constructor}}</span>
                </div>
                <div class="DataField">
                    <span class="df-title">质保条件</span>
                    <span class="df-value">{{d.qualityguarantee}}</span>
                </div>
            </div>
            <div class="col-lg-4 col-sm-6" obj-auth="true" obj-role="saler|buyer" obj-org="supplier_deptidstr|client_deptidstr" obj-action="Trade|Order|View">
                <h5>支付和发票信息</h5>
                <div class="DataField">
                    <span class="df-title">支付方式：</span>
                    <span class="df-value">{{d.settlement}}</span>
                </div>
                <div class="DataField">
                    <span class="df-title">发票抬头：</span>
                    <span class="df-value">{{d.invoiceheading}}</span>
                </div>
                <div class="DataField">
                    <span class="df-title">发票类型：</span>
                    <span class="df-value">{{d.invoicetype}}</span>
                </div>
                <div class="DataField">
                    <span class="df-title">税&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;率：</span>
                    <span class="df-value">{{d.taxrate}}</span>
                </div>
                <div class="DataField">
                    <span class="df-title">发票内容：</span>
                    <span class="df-value">{{d.invoicemx}}</span>
                </div>
            </div>
        </div>
        <div class="row">
            <h5>其他信息</h5>
            {{#if(d.src_orderserial){ }}
            <div class="DataField">
                <span class="df-title">客户订单：</span>
                <span class="df-value">{{d.src_orderserial}}</span>
            </div>
            {{#} }}
            <div class="DataField">
                <span class="df-title">备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注：</span>
                <span class="df-value">{{d.memo}}</span>
            </div>
        </div>
    </div>
</div>

<!--订单明细-->
@*<span id="chkGridStyle">启用现代列表样式</span>
    <br />*@
<div class="InfoSection row expand" id="orderItems">
    <div class="InfoSectionTitle">
        商品明细
        <i class="soicon "></i>
    </div>
    <div class="InfoSectionBody">

        <div class="order-summy">
            <div id="order-summy-count"><strong>商品数量：</strong><span></span></div>
            <br />
            <div id="order-summy-amount"><strong>商品总价：</strong><span></span></div>
            <br />
            <div id="order-summy-dlvfee"><strong>运费：</strong><span></span></div>
            <br />
            <div id="order-summy-dicount"><strong>折扣：</strong><span></span></div>
            <br />
            <div id="order-summy-pay"><strong>交易总额：</strong><span></span></div>
        </div>
    </div>
</div>
<script src="/LiberUI/Scripts/jquery.media.js"></script>
<script src="/LiberUI/Scripts/laytpl.js"></script>
<script src="/SiteUI/res_sobiz.js"></script>
<script>
    var order = {}
    var ViewOD = {
        tplTradeInfo:null,
        GetOrderEnd: function (rtn) {
            if (rtn.IsPositive) {
                order = rtn.Data;
                //#region 处理订单状态信息
                $("#oh-id").vl(order.idstr);
                if (order.src_orderserial) {
                    if (order.src_orderversion) {
                        order.src_orderserial += '[' + order.src_orderversion + ']';
                    }
                }
                else {
                    order.src_orderserial = "-";
                }
                $("#oh-clnt-serial").vl(order.src_orderserial);
                $("#oh-amount").vl(order.amount.roundTo(2).toThousands());
                $("#oh-order-date").vl(new Date(order.src_orderdate||order.createtime).toLocaleDateString());
                order.taxrate = order.taxrate + "%";

                $("#oh-status").addClass(order.status);
                $("#oh-status").vl("<i></i>" + res_sobiz.var.Purchase[order.status]);

                $("#lnkDelivery").attr("href","/Sale/DeliveryBillEdit?oid="+order.idstr).show();

                order.buyer_email = order.buyer_email || "-";
                order.saler_email = order.saler_email || "-";
                order.consignee_displayname = order.src_consignee ||order.consignee_realname;
                order.consignee_phone = order.consignee_phone || order.src_consigneephone;
                order._dlvDate = new Date(order.deliverydate||order.src_deliverydate).toString();
                order._urgent = daysBetween(new Date(order.deliverydate||order.src_deliverydate).toString(), new Date().toString()) < 5;
                order._constructor = order.src_constructor || "-";
                if (order.src_subconstructor) {
                    order._constructor += "(" + order.src_subconstructor + ")";
                }
                //#endregion

                if (order.Attachment) {
                    $("#tradeAttachemenTitle").vl("<i class='soicon icon-attachfile'></i>订单附件[" + order.Attachment.length + "]");
                    order.Attachment.forEach(function (a) {
                        var ae = $("<div class='Attachment'>");
                        var ae_icon = $("<b class='am-icon'>").appendTo(ae);
                        var ae_info = $("<div class='am-info'>").appendTo(ae);
                        var fn = a.filename;
                        var ftype = fn.split('.');
                        ftype = ftype[ftype.length - 1];
                        switch (ftype) {
                            case "pdf":
                                ae_icon.addClass("am-ftype-pdf");
                                break;
                            case "doc":
                            case "docx":
                                ae_icon.addClass("am-ftype-word");
                                break;
                        }
                        if (fn.indexOf("Order-Purchasecontract") > 0) {
                            fn = "采购合同:" + (order.src_orderserial || ("订单-" + order.idstr)) + "." + ftype;
                        }
                        var ae_name = $("<div class='am-info-name'>").vl(fn).appendTo(ae_info);
                        ae.attr("title", fn);
                        var ae_action = $("<div class='am-action'>").appendTo(ae_info);
                        $("<a><i class='soicon icon-xiazaidownload145'></i>下载</a>").attr("href", "/File/GetBizDoc?fileid=" + a.idstr+"&savename="+escape(fn)).appendTo(ae_action);
                        ae.appendTo("#tradeAttachment");
                    });
                }
                else {
                    $("#tradeAttachemenTitle").vl("订单附件[0]");
                    $("#tradeAttachment").remove();
                }

                var ti = ViewOD.tplTradeInfo.render(order);
                $("#tradeInfo").append(ti).fadeIn();

                //处理订单相关待办
                if (order.Todos) {
                    order.Todos.forEach(function (t) {
                        if (t.status !== "Aborted") {
                            $("#tradeAction").find("." + t.todotype).filter("." + t.status).fadeIn();
                        }
                    });
                }

                ViewOD.renderOrderMX();
                View.CheckAuthor(order);
                $("#pagePath").show();
            }
            else {
                error("错误", ErrFormate(res_sobiz, rtn.ErrorSrc, rtn.ErrorCode, rtn.Error), 5, function () { window.History.go(-1); });
            }
        },
        renderOrderMX: function () {
            //#region 处理订单明细
            if (order.OrderMX) {
                order.am = 0;
                order.tdlv = 0;
                order.tstlkin = 0;

                order.OrderMX.forEach(function (i) {
                    i._count = i.count + "(" + i.unit + ")";
                    i._price = "￥" + i.price;
                    i._amount = "￥" + i.amount;

                    order.am += i.amount;
                    order.tdlv += ($.isNumeric(i.totaldeliveried) ? i.totaldeliveried : 0) * i.price;
                    order.tstlkin += ($.isNumeric(i.totalstockin) ? i.totalstockin : 0) * i.price;
                })

                mxGrid.vl(order.OrderMX)
                      .update();


                $("#orderDelivery").children("span").eq(0).vl("已发货：" + (order.tdlv / order.am).formatPercent(2)).attr("title",order.tdlv);
                $("#orderDelivery").children("span").eq(1).vl("已入库：" + (order.tstlkin/order.am).formatPercent(2)).attr("title",order.tstlkin);

                $("#order-summy-count>span").vl(order.OrderMX.length);
                $("#order-summy-amount>span").vl('￥' + order.am.roundTo(2).toThousands());

                //TODO:处理订单附加费用等信息
                $("#order-summy-dlvfee>span").vl("-");
                $("#order-summy-discount>span").vl("-");
                $("#order-summy-pay>span").vl("￥" + order.am.roundTo(2).toThousands());
            }
            //#endregion
        },
        swithGridStyle: function (ev) {
            if (ev.Controller.vl() == 'unchecked') {
                $.Cookie("ordermx_style", "classic");
            }
            else {
                $.Cookie("ordermx_style", "moden");
            }
            ViewOD.renderOrderMX();
        },
        onInsecClick: function (ev) {
            $(this).parent(".InfoSection").toggleClass("expand");
        },
        confirmOrder: function (ev) {
            if (order) {
                $.getJSON("/Sale/ConfirmOrder", { oid: order.idstr }, function (rtn) {
                    if (rtn.IsPositive) {
                        window.history.go(0);
                    }
                    else {
                        error("操作失败", ErrFormate(res_sobiz, rtn.ErrorSrc, rtn.ErrorCode, rtn.Error),10);
                    }
                });
            }
        },
        remindSaler: function (ev) {
            if (order) {
                $.getJSON("/Purchase/RemindSaler", { orderid: order.idstr });
                $("#tradeAction").find(".ConfirmOrder,.Active").vl("已发送提醒给供应商");
            }
        }
    }

    $(function () {
        var oid = getUrlParam("oid");
        $("body").on("UserReady", function () {
            $.getJSON("/Trade/GetOrder", { "oid": oid }, ViewOD.GetOrderEnd);
        });

        $(".InfoSectionTitle").on("click", ViewOD.onInsecClick);
        ViewOD.tplTradeInfo = laytpl($("#tradeInfo")[0].innerHTML);
        $("#tradeInfo").vl("");
        //new LiberUI.CheckBox("#chkGridStyle").on("valueChanged", ViewOD.swithGridStyle)
        //                             .vl($.Cookie('ordermx_style') === 'moden' ? 'checked' : 'unchecked');

        //初始化明细列表
        mxGrid = new LiberUI.Table();
        mxGrid.Dom.prependTo($("#orderItems").children(".InfoSectionBody"));
        mxGrid.Col("品牌", "8%", false, null, "brand", null)
              .Col("名称", "10%", false, null, "product")
              .Col("型号", "12%", false, null, "model", null, "order-item-model")
              .Col("规格", "20%", false, null, "src_specification", null, "order-item-specification")
              .Col("数量", "12%", false, null, "_count")
              .Col("已入库", "8%", false, null, "totalstockin")
              .Col("单价", "10%", false, null, "_price")
              .Col("金额", "10%", false, null, "_amount")
              .Col("备注", "10%", false, null, "momo");

        $("#btnConfirmOrder").on("click", ViewOD.confirmOrder);
        $("#btnRemindSaler").on("click", ViewOD.remindSaler);
    })
</script>

